// pages/authorize/authorize.js
const app = getApp()
const {formatTime}=require('../../utils/util')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName:"",
    head_Photo:"",
    sex:"男",
    age:0,
    isshow: true,
    date: formatTime(new Date())
  },
  userName:'',
  head_Photo:'',
  login() {
    var that=this
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: async (file) => {
        this.userName=file.userInfo.nickName
        this.head_Photo=file.userInfo.avatarUrl
        that.setData({
          userName:file.userInfo.nickName,
          isshow:false,
        })
      }
    })
    
  },

  //获取选择的日期
  bindDateChange: function(e){
    this.setData({
      date: e.detail.value
    })
    //获取当前时间
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var n = timestamp * 1000;
    var date = new Date(n);
    var nowYear = date.getFullYear()

    let dateTime = e.detail.value
    // console.log(dateTime.slice(0,4))
    dateTime = dateTime.slice(0,4)
    let age= nowYear - dateTime
    console.log(age)
    this.setData({
      age:age
    })
  },

  //获取性别男
  selectBoy: function(e){
    console.log(e.currentTarget.dataset.text)
    let sex = e.currentTarget.dataset.text
    this.setData({
      sex: sex
    })
    
  },
  //获取性别女
  selectGirl: function(e){
    console.log(e.currentTarget.dataset.text)
    let sex = e.currentTarget.dataset.text
    this.setData({
      sex: sex
    })
  },

  //保存
  updateInfor: async function(){
    let token=wx.getStorageSync('token')
      wx.request({
        url: `${app.globalData.url}/customer/user`,
        method:"POST",
        header: {
          'content-type':'application/json',
          'Authorization':token
        },
        data:{
          head_Photo: this.head_Photo,
          userName: this.userName,
          sex: this.data.sex,
          age: this.data.age
        },
        success:(res)=>{
          let userInfo=res.data
          console.log(userInfo)
           // 本地存储成为user对象
          wx.setStorageSync('user', {
            userId: userInfo.userId,
            userName: userInfo.userName,
            sex: userInfo.sex,
            age: userInfo.age,
            head_Photo: userInfo.head_Photo
          })
          wx.switchTab({
            url: '/pages/home/home'
          })
        },
        fail:(err)=>{
          console.log(err)
        },
      })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },





  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})